package cn.tedu.manager.controller;

import cn.tedu.manager.pojo.dto.ManagerDTO;
import cn.tedu.manager.pojo.dto.ManagerQueryDTO;
import cn.tedu.manager.pojo.dto.ManagerResetPWDDTO;
import cn.tedu.manager.pojo.dto.ManagerSaveDTO;
import cn.tedu.manager.pojo.vo.ManagerPageListVO;
import cn.tedu.manager.pojo.vo.ManagerVO;
import cn.tedu.manager.service.ManagerService;
import cn.tedu.manager.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/v1/manager/")
@Api(tags = "管理员")
public class ManagerController {
    @Autowired
    private ManagerService managerService;

    @ApiOperation("列表(分页)")
    @GetMapping("list")
    public JsonResult list(ManagerQueryDTO managerQueryDTO) {
        ManagerPageListVO list = managerService.selectPage(managerQueryDTO);
        return JsonResult.ok(list);
    }

    @ApiOperation("查询单个管理员")
    @GetMapping("selectOne")
    public JsonResult selectOn(ManagerDTO managerDTO) {
        ManagerVO managerVO = managerService.selectOne(managerDTO);
        return JsonResult.ok(managerVO);
    }

    @ApiOperation("编辑管理员")
    @PostMapping("update")
    public JsonResult update(@RequestBody @Validated ManagerSaveDTO managerSaveDTO) {
        managerService.updateManager(managerSaveDTO);
        return JsonResult.ok();
    }

    @ApiOperation("新增管理员")
    @PostMapping("add")
    public JsonResult add(@RequestBody @Validated ManagerSaveDTO managerSaveDTO) {
        managerService.addManager(managerSaveDTO);
        return JsonResult.ok();
    }

    @ApiOperation("管理员详情")
    @GetMapping("detail")
    public JsonResult detail(ManagerDTO managerDTO) {
        ManagerVO managerVO = managerService.selectOne(managerDTO);
        return JsonResult.ok(managerVO);
    }

    @ApiOperation("修改管理员状态")
    @GetMapping("changeStatus")
    public JsonResult changeStatus(ManagerDTO managerDTO) {
        managerService.changeStatus(managerDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除管理员")
    @DeleteMapping("delete")
    public JsonResult delete(ManagerDTO managerDTO) {
        managerService.deleteManager(managerDTO);
        return JsonResult.ok();
    }

    @ApiOperation("重置密码")
    @PostMapping("resetPWD")
    public JsonResult resetPWD(@Validated @RequestBody ManagerResetPWDDTO managerResetPWDDTO) {
        managerService.resetPassword(managerResetPWDDTO);
        return JsonResult.ok();
    }
}
